﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO = Microsoft.MSE.Catalog.Contracts;

namespace Microsoft.MSE.Catalog.Module.Data
{
    public class MessagePart
    {
        public MessagePart()
        {

        }

        public static void CreateMessagePart(DTO.MessagePart part, Guid operationVersionID, int ordinal, bool isRequestMsg, SqlTransaction txn, SqlConnection conn)
        {
            Guid ID;
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[CreateMessagePart]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                    SqlParameter OperationVersionIDParam = new SqlParameter("@OperationVersionID", SqlDbType.UniqueIdentifier);
                    SqlParameter IsRequestMsgParam = new SqlParameter("@IsRequestMsg", SqlDbType.Bit);
                    SqlParameter OrdinalParam = new SqlParameter("@Ordinal", SqlDbType.Int);
                    SqlParameter SchemaObjectIDParam = new SqlParameter("@SchemaObjectID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Direction = ParameterDirection.Output;
                    OperationVersionIDParam.Value = operationVersionID;
                    SchemaObjectIDParam.Value = part.SchemaObject.ID;
                    if (((Guid)SchemaObjectIDParam.Value) == Guid.Empty)
                    {
                        throw new ApplicationException("Attempt to create message part with invalid SchemaObject. ID is NULL");
                    }
                    OrdinalParam.Value = ordinal;
                    IsRequestMsgParam.Value = isRequestMsg;

                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(OperationVersionIDParam);
                    cmd.Parameters.Add(SchemaObjectIDParam);
                    cmd.Parameters.Add(OrdinalParam);
                    cmd.Parameters.Add(IsRequestMsgParam);

                    cmd.ExecuteNonQuery();
                    ID = (Guid)IDParam.Value;
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

        }

        public static void DeleteMessagesFromOperationVersion(Guid ID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[DeleteMessagesFromOperationVersion]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    cmd.Parameters.Add(IDParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }
        }

        public static List<DTO.MessagePart> GetMessage(Guid ID, bool isRequestMsg, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            List<DTO.MessagePart> message = new List<DTO.MessagePart>();
            const string sprocname = "[ServiceEngine].[GetMessage]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@OperationVersionID", SqlDbType.UniqueIdentifier);
                    SqlParameter IsRequestMsgParam = new SqlParameter("@IsRequestMsg", SqlDbType.Bit);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    IsRequestMsgParam.Value = isRequestMsg;
                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(IsRequestMsgParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                DTO.MessagePart part = new DTO.MessagePart();
                                part.SchemaObject = SchemaObject.GetSchemaObject(reader.GetGuid(2),txn,conn);
                                message.Add(part);
                            }
                        }
                    }
                }


            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection if opened in this method
            }

            return message;

        }

    }
}
